package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 祸不单行<br/>
 * 一次强力攻击，对目标造成{0}%技能伤害，并对降低敌方所有目标{1}%技能防御力
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90116 extends CombatSkill {

	public COMBAT_SKILL_90116(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		CombatUnit target = action.target.getDefenseUnit(source.getIndex());
		if (target != null) {
			ActionEffect effect = action.addTarget(target);
			target = effect.getUnit(); // 修正目标
			float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
			CombatUtils.calcMagicAttack(effect, rate);
		}
		List<CombatUnit> targets = action.target.all();
		for (int i = 0; i < targets.size(); i++) {
			CombatUnit u = targets.get(i);
			if (!u.isDied()) {
				action.addBuff(new BUFF(skill, u));
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		int oldDefense;
		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner);
			active = true;
			int defense = (int) owner.getDefense();
			oldDefense = defense;
			defense -= defense * skill.getParameter(0) / GameStaticConfig.percentBase;
			owner.setAttributeValue(CombatAttributeType.MAGIC_DEFENSE, defense);
		}
		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_REMOVED) {
				owner.setAttributeValue(CombatAttributeType.MAGIC_DEFENSE, oldDefense);
			}
		}
		@Override
		@JSONField(serialize = false)
		public boolean isDebuff() {
			return true;
		}
	}

}
